package com.mmall.mmall002.controller;


import com.mmall.mmall002.entity.Orders;
import com.mmall.mmall002.entity.User;
import com.mmall.mmall002.service.CartService;
import com.mmall.mmall002.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙菲
 * @since 2021-03-02
 */
@Controller
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CartService cartService;

    /**
     * 添加订单的操作
     */
    @PostMapping("/settlement3")
    public ModelAndView settlement3(Orders o, HttpSession session,String address,String remark){


        //获得登录的用户
        User user = (User) session.getAttribute("user");
        orderService.save(o,user,address,remark);
        ModelAndView modelAndView  = new ModelAndView();
        modelAndView.setViewName("settlement3");
        if(user == null){
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",new ArrayList<>());
        }else{
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
            modelAndView.addObject("orders",o);
        }
        return modelAndView;

    }


    /**
     * 根据登录用户查询我的订单
     */
    @GetMapping("/orderList")
    public ModelAndView orderList(HttpSession session){

        //获得登录的用户
        User user = (User)session.getAttribute("user");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orderList");
        modelAndView.addObject("orderList",orderService.findAllOrdersByUserId(user));
        //根据用户编号查询出来购物车集合
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }
}

